/**
 * build_project
 * <p>
 * Copyright 2014 cayden.cn, Inc. All rights reserved.
 *
 * @author Administrator
 * @date 2021-07-01 09:08
 */
package com.cayden.project.componets.mybatis;

import com.github.pagehelper.autoconfigure.MapperProperties;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Configuration;
import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.mapperhelper.MapperHelper;

import javax.annotation.PostConstruct;
import java.util.List;

/**
 *
 * @author cayden
 * @date 2021-07-01 09:08
 */
@Configuration
@EnableConfigurationProperties({MapperProperties.class})
public class MapperConfig {

    private final List<SqlSessionFactory> sqlSessionFactoryList;

    @Autowired
    private MapperProperties mapperProperties;

    @Autowired
    private ApplicationContext applicationContext;

    public MapperConfig(List<SqlSessionFactory> sqlSessionFactoryList) {
        this.sqlSessionFactoryList = sqlSessionFactoryList;
    }

    @PostConstruct
    public void addPageInterceptor(){
        MapperHelper mapperHelper = new MapperHelper();
        mapperHelper.setConfig(mapperProperties);
        if (mapperProperties.getMappers().size() > 0){
            for (Class mapper : mapperProperties.getMappers()) {
                applicationContext.getBeansOfType(mapper);
                mapperHelper.registerMapper(mapper);
            }
        }else{
            applicationContext.getBeansOfType(Mapper.class);
            mapperHelper.registerMapper(Mapper.class);
        }

        for (SqlSessionFactory sqlSessionFactory : sqlSessionFactoryList) {
            mapperHelper.processConfiguration(sqlSessionFactory.getConfiguration());
        }

    }

}
